<extend name="Base:main" />
<block name="title">
<title>蔬菜外卖 - 订单列表</title>
</block>
<block name="main-content">
<div class="row">
	<div class="box col-md-12">
		<div class="box-inner">
			<div class="box-header well" data-original-title="">
				<h2>
					<i class="glyphicon glyphicon-user"></i>订单列表
				</h2>

				<div class="box-icon">
					<a href="#" class="btn btn-minimize btn-round btn-default">
						<i class="glyphicon glyphicon-chevron-up"></i></a> 
					<a href="#" class="btn btn-close btn-round btn-default">
						<i class="glyphicon glyphicon-remove"></i></a>
				</div>
			</div>
			<div class="box-content">
				<div class="alert alert-info">
					对订单进行查看、管理以及
					<button id="choose" type="button" class="btn  btn-success" onclick="choose(0)">配送</button>
					<button id="choose" type="button" class="btn  btn-success" onclick="choose(1)">完成配送</button>
					<button id="choose" type="button" class="btn  btn-success" onclick="choose(2)">收款</button>
				</div>
				<div class="loading col-sm-12 center">
					<img src="__PUBLIC__/img/ajax-loaders/ajax-loader-5.gif">
				</div>
				<table
					class="table table-striped table-bordered bootstrap-datatable datatable responsive"
					id="orderlist" style="display: none;">
					<thead>
						<tr>
							<th><input type="checkbox" id="all" />订单号</th>
							<th>订单商家</th>
							<th>订单总价</th>
							<th>订单配送区域</th>
							<th>下单日期</th>
							<th>下单时间</th>
							<th>支付方式</th>
							<th>状态</th>
							<th>订单配送时间</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody id="tbody">
						<foreach name="list" item="v">
						<tr>
							<td><a href="javascript:void(0);">{$v.id}</a></td>
							<td>{$v.account}</td>
							<td>{$v.food_price}元</td>
							<td>{$v.area}</td>
							<td>{$v.cdate}</td>
							<td>{$v.ctime}</td>
							<td><if condition="$v.payment eq 1">在线支付<else />货到付款</if></td>
							<td>
								<switch name="v.status" >
									<case value="0">未配送</case>
									<case value="1">配送中</case>
									<case value="2">已配送</case>
									<case value="3">已收款</case>
								</switch>
							</td>
							<td>{$v.ptime}</td>
							<td><label style="height: 26px;"><a
									href="javascript:details('{$v.id}');">查看详情 ></a></label></td>
						</tr>
						</foreach>
					</tbody>
					<tfoot>
						<tr>
							<th style="visibility: hidden;">订单号</th>
							<th >订单商家</th>
							<th style="visibility: hidden;">订单总价</th>
							<th>订单配送区域</th>
							<th>下单日期</th>
							<th style="visibility: hidden;">下单时间</th>
							<th>支付方式</th>
							<th>状态</th>
							<th>订单配送时间</th>
							<th style="visibility: hidden;">操作</th>
						</tr>
					</tfoot>
				</table>
			</div>
			<!-- end box-content -->
		</div>
		<!-- end box-inner -->
	</div>
	<!-- end box -->
</div>
<!-- end row --> <!-- 选择配送员弹出框 -->
<div class="modal fade" id="ChooseModal" tabindex="-1" role="dialog"
	aria-labelledby="markiModal" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabel">配送</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal style-form" id="form1"
					action="__APP__/Admin/Order/choose" method="post">
					<div class="form-group">
						<label class="col-sm-3 control-label">配送时间：</label>
						<div class="col-sm-9 ">
							<select name="ptime" class="form-control">
								<foreach name="ptimes" item="ptime">
								<option value="{$ptime}">{$ptime}</option>
								</foreach>
							</select>
						</div>
						<div id="orders" style="display: none;"></div>
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				<button type="button" class="btn btn-danger">确认</button>
			</div>
		</div>
	</div>
</div>
<!-- end row --> <!-- 订单详情弹出框 -->
<div class="modal fade" id="orderDetailsModal" tabindex="-1"
	role="dialog" aria-labelledby="markiModal" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content" id="detail_content">
		</div>
	</div>
</div>
</block>
<block name="js-content"> <!-- 日期js --> <script type="text/javascript">
  var lastWay = -1;
  var geturl = "__APP__/Admin/Order/getData";
  var detailsUrl = "__APP__/Admin/Order/details";
  var ChooseUrl = "__APP__/Admin/Order/choose";
  $(function(){
//	  var date = "<?php echo date('Y-m-d'); ?>";
//       $('#startTime').val(date);
//       $('#startTime').datetimepicker({lang:'ch'});
//       $('#startTime').datetimepicker({
//         timepicker:false,
//         format:'Y-m-d'
//       });
  });
  function details(id){
		$.post(detailsUrl,{id:id},function(data){
			$("#detail_content").html(data);
		});
		$("#orderDetailsModal").modal('show');
 }
  $("#all").click(function(){
	var isChecked = this.checked;
	$("input[type='checkbox']").each(function(){
		this.checked = isChecked;
	}); 
  });
  $("#choose1").click(function(){
// 	  $("input[name='sh']").attr("checked",this.checked);
	  var checkboxes = $("input[name='check[]']");
	  var count = checkboxes.length;
	  var str = "";
	  var num = 0;
	  $('#orders').html("");
	  for(i=0;i<count;i++){
		  var checkbox = checkboxes[i];
		  if(checkbox.checked){
			  var input = "<input type='text' name='orderId[]' value='"+checkbox.value+"'/>";
			  $('#orders').append(input);
			  str += checkbox.value+",";
			  num++;
		  }
	  }
//	  console.log('num:'+num);
//	  if(num ==0){
//		  alert("请先选择一个订单");
//	  }else{
//		  $("#ChooseModal").modal('show');
//	  }
	  table.rows().every( function () {
		  var data = this.data();
		  $checkbox = $(data[0]);
		  if(data[7] == "未配送"){
			  data[0] = '<input type="checkbox" value="{$v.id}" name="check[]" />'+$checkbox[$checkbox.length-1].outerHTML;
		  }else{
			  data[0] = $checkbox[$checkbox.length-1].outerHTML;
		  }
		  this.data(data);
	  });
// 	  console.log("checkbox:"+str);
// 	  console.log("checkboxNum:"+num);
	});
  function choose(way){
	  var status = "";
		//是否两次点击同一个按钮
	  if(lastWay != way){
		  lastWay = way;
		  switch(way){
		  case 0:
			  status = "未配送";
			  break;
		  case 1:
			  status = "配送中";
			  break;
		  case 2:
			  status = "已配送";
			  break;
		  }
		  table.rows().every( function () {
			  var data = this.data();
			  $checkbox = $(data[0]);
			  var oid = $checkbox[$checkbox.length-1].outerText;
			  if(data[7] == status && (way !=2 || data[6] == "货到付款")){
				  data[0] = '<input type="checkbox" value="'+oid+'" name="check[]" />'+$checkbox[$checkbox.length-1].outerHTML;
			  }else{
				  data[0] = $checkbox[$checkbox.length-1].outerHTML;
			  }
			  this.data(data);
		  });
	  }else{
		  var checkboxes = $("input[name='check[]']");
		  var count = checkboxes.length;
		  var str = "";
		  for(i=0;i<count;i++){
			  var checkbox = checkboxes[i];
			  if(checkbox.checked){
				  str += checkbox.value+",";
			  }
		  }
		  if(str == ""){
			  alert("请先选择一个订单");
			  return;
		  }
		  console.log(str);
		  str = str.substr(0,str.length-1);
		  var r=confirm("是否确定要更改订单状态？");
		  if(r){
			  $.post(ChooseUrl,{oids:str,status:way},function(data){
				  console.log(data);
				  if(data){
					  alert("修改成功");
					  window.location.reload();
				  }else{
					  alert("修改失败");
				  }
			  });
		  }
	  }
  }
  $('#ChooseModal').on('click','button.btn-danger',function (a){
// 		var id = $(".btn-danger").attr('id');
// 		var name = $("#name").val();
// 		$.post(editUrl,{id:id,action:action,name:name},function(data){
// //	 		console.log(data);
// 			if(data){
// 				alert('修改成功');
// 				$("#name_"+id).html(name);
// 				$("#myEditModal").modal('hide');
// 			}else{
// 				alert('修改失败');
// 			}
// 		});
	  $("#form1").submit();
// 		window.location.href= deleteUrl+"?action="+action+"&id="+this.id; 
	});
    function aChange(){
      var _tbody = document.getElementById("tbody");
      var time = $("#startTime").val();
      var area = $("#oarea").val();
      var status = $("ostatus").val();
      $.post(geturl,{time:time,area:area,status:status},
          function(data){
                  for(var i = _tbody.childNodes.length - 1; i >= 0 ;i--){
                    _tbody.removeChild(_tbody.childNodes[i]);
                  }
                  for(var i = 0; i < data.length ;i++){
                      var _tr = document.createElement("tr");
                      var td1 =  _tr.insertCell();
                      td1.innerHTML = data[i]['order_number'];

                      var td2 =  _tr.insertCell();
                      td2.innerHTML = data[i]['username'];

                      var td3 =  _tr.insertCell();
                      td3.innerHTML = data[i]['sum_price'];

                      var td4 =  _tr.insertCell();
                      td4.innerHTML = data[i]['stime'];

                      var td5 =  _tr.insertCell();
                      td5.innerHTML = data[i]['area'];

                      var td6 =  _tr.insertCell();
                      td6.innerHTML = data[i]['ctime'];

                      var td7 =  _tr.insertCell();
                      td7.innerHTML = data[i]['未配送'];

                      var td8 =  _tr.insertCell();
                      td8.innerHTML = "<label style=\"height:26px;\"><a href=\"{:U('Order/details',array('order_number'=>$v['order_number']))}\">查看详情　></a></label>";
                      _tbody.appendChild(_tr);
                  }
          }
        );

    }
    $("#search").click(function(){
      var startTime = $("#startTime").val();
      var endTime   = $("#endTime").val();
      var content   = $("#content").val();
      if(startTime!=""&&endTime==""){
        alert("请选择开始时间和结束时间");
      }
      else if(startTime==""&&endTime!=""){
        alert("请选择开始时间和结束时间");
      }
      else if(startTime==""&&endTime==""&&content==""){
        alert("搜索内容不能为空");
      }
      else{
        searchForm.submit();
      }

    });
  </script> </block>